package com.pdsu.alumni.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.annotation.NoAuthentication;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.*;
import com.pdsu.alumni.service.*;
import com.pdsu.alumni.util.TencentMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @program: alumni
 * @description: 用户UI界面控制层
 * @author: Acerola
 * @create: 2021-11-12 18:04
 **/

@Api(tags = "新闻控制层(用户UI界面)")
@RestController
@RequestMapping("/messageUI")
public class MessageControllerUserUI {

    @Autowired
    private IMessageService messageService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserService userService;
    @Autowired
    private IMessageTypeService messageTypeService;
    @Autowired
    private TencentMessage tencentMessage;
    @Autowired
    private ISlideshowService slideshowService;
    @Autowired
    private IFilesService filesService;
    @Autowired
    private ICoverService coverService;

    @ApiOperation(value = "获取所有新闻(用户ui)")
    @GetMapping("/getList")
    @ResponseBody
    @CrossOrigin
    @NoAuthentication
    public Result<?> getAllMessage() {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", 1);
        List<Message> list = messageService.list(wrapper);
        //按发布日期倒序
        list.sort(Comparator.comparing(Message::getSendTime));
        Collections.reverse(list);

        System.out.println(list);
        list.forEach(item -> {
            QueryWrapper<Cover> coverQueryWrapper = new QueryWrapper<>();
            coverQueryWrapper.eq("mid", item.getId());
            coverQueryWrapper.eq("deleted", 1);
            Cover cover = coverService.getOne(coverQueryWrapper);
            System.out.println(item.getId());
            System.out.println("对应的封面是" + cover);
            item.setImgSrc(cover.getCoverUrl());
        });
        return new Result<>().success().put(list);
    }

    @ApiOperation(value = "按分类获取新闻列表(用户ui)")
    @GetMapping("/getListByType")
    @ResponseBody
    @CrossOrigin
    @NoAuthentication
    public Result<?> getMessageListByMessageType(Integer id) {
        QueryWrapper<Message> messageQueryWrapper = new QueryWrapper<>();
        MessageType messageType = messageTypeService.getById(id);
        messageQueryWrapper.eq("type_path", messageType.getTypePath());
        List<Message> list = messageService.list(messageQueryWrapper);
        return new Result<>().success().put(list);
    }

    @ApiOperation(value = "获取首页轮播图")
    @GetMapping("/slideshow")
    @ResponseBody
    @CrossOrigin
    @NoAuthentication
    public Result<?> getSlideshow() {
        QueryWrapper<Slideshow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 1);
        queryWrapper.eq("s_type", 0);
        List<Slideshow> list = slideshowService.list(queryWrapper);
        return new Result<>().success().put(list);
    }

    @ApiOperation(value = "根据id获取新闻内容")
    @GetMapping("/examine")
    @ResponseBody
    @CrossOrigin
    public Result<?> getMessageById(Integer id) {
        Message message = messageService.getById(id);
        String str = message.getTypePath();
        String[] res = str.split("/");
        ArrayList<String> list2 = new ArrayList<String>();
        int sum = res.length;
        String p = null;
        if (sum == 0) {
            list2.add("/");
        } else {
            for (int j = 1; j < sum; j++) {
                for (int k = 1; k <= j; k++) {
                    if (k == 1) {
                        p = "/" + res[k];
                    } else
                        p += "/" + res[k];
                }
                list2.add(p);
            }
        }
        message.setPathList(list2);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("message", message);
        //封面文件读取
        QueryWrapper<Cover> coverQueryWrapper = new QueryWrapper<Cover>();
        coverQueryWrapper.eq("mid", id);
        coverQueryWrapper.eq("deleted", 1);
        Cover cover = coverService.getOne(coverQueryWrapper);
        hashMap.put("cover", cover);
        //附件读取
        QueryWrapper<Files> filesQueryWrapper = new QueryWrapper<>();
        filesQueryWrapper.eq("mid", id);
        filesQueryWrapper.eq("deleted", 1);
        List<Files> filesList = filesService.list(filesQueryWrapper);
        hashMap.put("fileList", filesList);
        return new Result<>().success().put(hashMap);
    }

    @ApiOperation(value = "获取所有新闻类别")
    @GetMapping("/getAllType")
    @NoAuthentication
    @ResponseBody
    @CrossOrigin
    public Result<?> getAllMessageType() {
        List<MessageType> tree = messageTypeService.Tree();
        return new Result<>().success().put(tree);
    }

}
